from rest_framework import serializers
from user import models
import re


def verify(value):
    ser = re.match(r'^1[3-9]\d{9}$',value)
    if ser:
        data = models.User.objects.filter(phone=value)
        if data:
            raise serializers.ValidationError("手机号重复不能添加")
        else:
            return value
    else:
        raise serializers.ValidationError("手机号不符合规则")


class UserSer(serializers.ModelSerializer):
    class Meta:
        model = models.User
        fields = '__all__'
        extra_kwargs = {"phone": {"required": True, "validators": [verify]}}
